package admin

import (
	"fmt"
	"github.com/kataras/iris/v12"
	"iris_blog/utils"
	"strings"
)

type Users struct {
}

// 登录 接口
func (u Users) Login(i iris.Context) {
	var res map[string]interface{}
	i.ReadJSON(&res)
	fmt.Println(res["username"], res["password"])

	var result []map[string]interface{}
	utils.DB.Raw("SELECT id FROM users WHERE username = ? and password = ?;", res["username"], res["password"]).Scan(&result)
	if len(result) == 0 {
		i.JSON(map[string]interface{}{
			"code": 20001,
			"data": "登录失败",
		})
		return
	}
	fmt.Println(result, "====login====")
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": map[string]interface{}{
			"token": "xjkdjkadyauydua7897846dhjadakdjkal",
			"res":   result,
		},
	})
}

// 拉取权限和个人信息
func (u Users) Info(i iris.Context) {
	var res map[string]interface{}
	i.ReadJSON(&res)
	fmt.Println(res, "------info----")

	result := make(map[string]interface{})
	utils.DB.Raw("SELECT * FROM users WHERE id= ?", res["id"]).Find(&result)
	fmt.Println(result)

	results := make(map[string]interface{})
	utils.DB.Raw("SELECT * FROM role WHERE id = ?", result["role_id"]).Find(&results)
	fmt.Println(results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": map[string]interface{}{
			"name":   result["username"],
			"avatar": result["avatar"],
			"roles":  strings.Split(results["role"].(string), ","),
		},
	})
}

// 退出接口
func (u Users) Logout(i iris.Context) {

	i.JSON(map[string]interface{}{
		"code": 20000,
		"登录":   "退出成功",
	})
}

// 用户列表展示
func (u Users) GetUserList(i iris.Context) {
	results := []map[string]interface{}{}
	utils.DB.Raw("SELECT username,title,avatar,users.id,role_id FROM users LEFT JOIN role r on r.id = users.role_id").Find(&results)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
	})
}

// 添加用户
func (u Users) AddUser(i iris.Context) {
	result := map[string]interface{}{}
	i.ReadJSON(&result)
	fmt.Println(result)
	results := map[string]interface{}{}
	utils.DB.Raw("INSERT INTO users (avatar,password,role_id,username) values (?,?,?,?)", result["img"], result["password"], result["role"], result["username"]).Find(&results)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": results,
		"msg":  "添加用户成功",
	})
}

// 删除用户
func (u Users) DelUser(i iris.Context) {
	result := map[string]interface{}{}
	i.ReadJSON(&result)
	fmt.Println(result["id"])
	results := map[string]interface{}{}
	utils.DB.Raw("DELETE FROM users WHERE id=?", result["id"]).Find(&results)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": "删除成功",
	})
}

// 修改用户
func (u Users) EditUser(i iris.Context) {
	result := map[string]interface{}{}
	i.ReadJSON(&result)
	fmt.Println(result)

	results := map[string]interface{}{}
	utils.DB.Raw("UPDATE users SET avatar=?,username=?,password=?,role_id=? WHERE id = ?", result["img"], result["username"], result["password"], result["role"], result["id"]).Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"msg":  "修改用户成功",
	})
}

// 角色列表展示
func (u Users) GetRoleList(i iris.Context) {
	result := []map[string]interface{}{}
	utils.DB.Raw("SELECT title,role,`desc`,id FROM role").Find(&result)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"data": result,
	})
}

// 添加角色
func (u Users) AddRole(i iris.Context) {
	result := map[string]interface{}{}
	i.ReadJSON(&result)
	fmt.Println(result)

	results := map[string]interface{}{}
	utils.DB.Raw("INSERT INTO role ( title, role, add_time, `desc`) VALUES (?,?,?,?)", result["title"], result["role"], utils.GetDate(), result["desc"]).Find(&results)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"msg":  "添加用户成功",
	})
}

// 修改角色
func (u Users) EditRole(i iris.Context) {
	result := map[string]interface{}{}
	i.ReadJSON(&result)
	fmt.Println(result)
	results := map[string]interface{}{}
	utils.DB.Raw("UPDATE role SET title=?,`desc`=?,role=? WHERE id = ?", result["title"], result["desc"], result["role"], result["id"]).Find(&results)
	i.JSON(map[string]interface{}{
		"code": 20000,
		"msg":  "修改用户成功",
	})
}

// 删除角色
func (u Users) DelRole(i iris.Context) {
	result := map[string]interface{}{}
	i.ReadJSON(&result)
	fmt.Println(result["id"])
	results := map[string]interface{}{}
	utils.DB.Raw("DELETE FROM role WHERE id=?", result["id"]).Find(&results)
	//utils.DB.Raw("DELETE FROM role WHERE id=?",).Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"msg":  "删除用户成功",
	})
}

// 记录用户信息
func (u Users) AdduserInformation(i iris.Context) {
	result := map[string]string{}
	result = i.URLParams()
	fmt.Println(i.URLParam("userAgent"),"-------------")
	fmt.Println(result)
	fmt.Println(result["cip"])
	results := map[string]interface{}{}
	utils.DB.Raw("INSERT INTO UserRecords (cip, cname, cid, userAgent, add_time) VALUES (?,?,?,?,?)", result["cip"], result["cname"], result["cid"], result["userAgent"], utils.GetUnix()).Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"msg":  "记录成功",
	})
}

func (u Users) GetUserInformationList(i iris.Context)  {

	results := []map[string]interface{}{}
	utils.DB.Raw("SELECT * FROM UserRecords ORDER BY add_time desc").Find(&results)

	i.JSON(map[string]interface{}{
		"code": 20000,
		"data":results,
		"msg":  "记录成功",
	})
}
